package com.fsnbigdata.web;
import com.fsnbigdata.domain.AnimalConservation;
import com.fsnbigdata.enums.ResultEnum;
import com.fsnbigdata.exception.ParamException;
import com.fsnbigdata.util.ReflectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.roo.addon.web.mvc.controller.annotations.ControllerType;
import org.springframework.roo.addon.web.mvc.controller.annotations.RooController;
import org.springframework.roo.addon.web.mvc.controller.annotations.responses.json.RooJSON;
import com.fsnbigdata.service.api.AnimalConservationService;
import io.springlets.web.NotFoundException;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
import org.springframework.web.util.UriComponents;
import springfox.documentation.annotations.ApiIgnore;

import java.lang.reflect.Method;

/**
 * = AnimalConservationsItemJsonController
 *
 * TODO Auto-generated class documentation
 *
 */
@RooController(entity = AnimalConservation.class, pathPrefix = "/api", type = ControllerType.ITEM)
@RooJSON
@RestController
@RequestMapping(value = "/api/animalconservations/{id}", name = "AnimalConservationsItemJsonController", produces = MediaType.APPLICATION_JSON_VALUE)
@Api(tags = "AnimalConservations", description = "动物保育" )
public class AnimalConservationsItemJsonController {

    /**
     * TODO Auto-generated attribute documentation
     *
     */
    private AnimalConservationService animalConservationService;

    /**
     * TODO Auto-generated constructor documentation
     *
     * @param animalConservationService
     */
    @Autowired
    public AnimalConservationsItemJsonController(AnimalConservationService animalConservationService) {
        this.animalConservationService = animalConservationService;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @return AnimalConservationService
     */
    public AnimalConservationService getAnimalConservationService() {
        return animalConservationService;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param animalConservationService
     */
    public void setAnimalConservationService(AnimalConservationService animalConservationService) {
        this.animalConservationService = animalConservationService;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param id
     * @return AnimalConservation
     */
    @ModelAttribute
    public AnimalConservation getAnimalConservation(@PathVariable("id") Long id) {
        AnimalConservation animalConservation = animalConservationService.findOne(id);
        if (animalConservation == null) {
            throw new NotFoundException(String.format("AnimalConservation with identifier '%s' not found", id));
        }
        return animalConservation;
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param animalConservation
     * @return ResponseEntity
     */
    @GetMapping(name = "show")
    //@PreAuthorize("hasRole('USER')")
    public ResponseEntity<?> show(@ApiIgnore @ModelAttribute AnimalConservation animalConservation) {
        return ResponseEntity.ok(animalConservation);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param animalConservation
     * @return UriComponents
     */
    public static UriComponents showURI(AnimalConservation animalConservation) {
        return MvcUriComponentsBuilder.fromMethodCall(MvcUriComponentsBuilder.on(AnimalConservationsItemJsonController.class).show(animalConservation)).buildAndExpand(animalConservation.getId()).encode();
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param storedAnimalConservation
     * @param animalConservation
     * @param result
     * @return ResponseEntity
     */
    @PutMapping(name = "update")
    @PreAuthorize("hasRole('DB_ES_CS')")
    @ApiOperation(value = "",notes = "update")
    public ResponseEntity<?> update(@ApiIgnore @ModelAttribute AnimalConservation storedAnimalConservation, @Valid @RequestBody AnimalConservation animalConservation, BindingResult result) throws Exception {
        if (result.hasErrors()) {
            throw new ParamException(ResultEnum.PARAM_ERROR.getCode(),
                    result.getFieldError().getDefaultMessage());
        }
        animalConservation.getLocation().forEach(it -> it.setAnimalConservation(animalConservation));
        animalConservation.getImages().forEach(it -> it.setAnimalConservation(animalConservation));
        animalConservation.setId(storedAnimalConservation.getId());

        String name =  getAnimalConservationService().findOne(storedAnimalConservation.getId()).getName();
        AnimalConservation animalConservation1 = getAnimalConservationService().save(animalConservation);
        Class<?> clazz = AnimalConservationsItemJsonController.class;
        Method method = clazz.getMethod("update", ReflectUtil.getPeremeterClass(clazz, "update"));
        ReflectUtil.setAnnotationOn(method,"更新动物保育，中文名："+name);

        return ResponseEntity.ok(animalConservation1);
    }

    /**
     * TODO Auto-generated method documentation
     *
     * @param animalConservation
     * @return ResponseEntity
     */
    @DeleteMapping(name = "delete")
    @PreAuthorize("hasRole('DB_ES_CS')")
    @ApiOperation(value = "",notes = "delete")
    public ResponseEntity<?> delete(@ApiIgnore @ModelAttribute AnimalConservation animalConservation) throws Exception {
        getAnimalConservationService().delete(animalConservation);

        Class<?> clazz = AnimalConservationsItemJsonController.class;
        Method method = clazz.getMethod("delete", ReflectUtil.getPeremeterClass(clazz, "delete"));
        ReflectUtil.setAnnotationOn(method,"删除动物保育，中文名："+animalConservation.getName());
        return ResponseEntity.ok().build();
    }
}
